$(document).ready(function(){
	//global vars
	var form = $("#loginForm");
	var name = $("#username");
	var nameInfo = $("#nameInfo");
	var loading=$("#loading");
	var pass = $("#password");
	var passInfo = $("#passInfo");
	var url="a_login.php";
	//Get Cookie;
	var remember = $.cookie('remember');
	if ( remember == 'true' ) {
		name.val($.cookie('username'));
	}
	$("#abc").css({height:$(window).height()-212});
	
	//On blur
	name.focusout(validateName);
	pass.focusout(validatePass);

	//On Submitting
	$("#btnLogin").click(function(){
		login();
	});
	
	$("#password,#username").keypress(function(e){
		//alert(e.which);
		if( e.which==13 ){
			login();
		}
	});
	function login(){
	if(validateName() & validatePass()){
			loading.addClass("info_loading").fadeIn("200");
			 $.post(url,  {'m':'c','username':name.val(),'password':pass.val()} , function(data){
				 //alert(data);
				var obj=eval("(" + data + ")");
				
				//Success
				if(obj['result']=="1"){
				
					if ($('#remember').attr('checked')) {
							var username = name.val();
							// set cookies to expire in 256 days
							$.cookie('username', username, { expires: 256 });
							$.cookie('remember', true, { expires: 256 });
					} else {
							// reset cookies
							$.cookie('username', null);
							$.cookie('remember', null);
					}
					
					//submit form
					form.submit();
					
				}else{ // Error
					loading.removeClass("info_loading");
					if (obj['result']=="2"){
								passInfo.addClass("info_error").fadeIn("slow");
								passInfo.text(obj['msg']);
					}else{
								nameInfo.addClass("info_error").fadeIn("slow");
								nameInfo.text(obj['msg']);
					}
				}
				//return true;
			 });
		} 
	}
	//validation functions
	function validateName(){
		//if it's NOT valid
		if(name.val().length < 1){
			nameInfo.addClass("info_error").fadeIn("slow");
			nameInfo.text("Please input username");
			return false;
		} 
		else {
			nameInfo.removeClass("info_error");//.addClass("info_success").show("slow");
			nameInfo.text("");
			return true;
		}

	}
	function validatePass(){
		//it's NOT valid
		if(pass.val().length < 1){
			passInfo.addClass("info_error").fadeIn("slow");
			passInfo.text("Please input password");
			return false;
		} else{
			passInfo.removeClass("info_error");//.addClass("info_success").show("slow");
			passInfo.text("");
			return true;
		}

	}
	
	function validateEmail(){
		//testing regular expression
		var a = $("#email").val();
		var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
		//if it's valid email
		if(filter.test(a)){
			$("#emailInfo").show("slow");
			emailInfo.text("Valid E-mail");
			return true;
		}
		//if it's NOT valid
		else{
			$("#emailInfo").show("slow");
			emailInfo.text("Type a valid e-mail please");
			return false;
		}
	}
	$('.close').click(function() {
		$(this).parent('.box').slideUp('slow');
	});
});
